//
//  SYS_GoodsDetailVC.m
//  System_App
//
//  Created by czy001 on 2017/12/6.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "SYS_GoodsDetailVC.h"
//#import "DSMallHotDetailViewModel.h"
//#import "DSMallHotDetailModel.h"
#import "DGHLoadWebView.h"
//#import "WJShareCustomView.h"
//#import "DSCustomerService.h"

@interface SYS_GoodsDetailVC ()<UIWebViewDelegate,CAAnimationDelegate>
{
    CGPoint center;
    CALayer     *layer;
}
//@property (nonatomic,strong) DSMallHotDetailViewModel *viewModel;
//@property (nonatomic,strong) DSMallHotDetailModel *model;
@property (nonatomic,strong) DGHLoadWebView *loadWebViewLayer;
@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic,strong) UIView *coverView;
@property (nonatomic,strong) UIBezierPath *path;
//分享视图
//@property (nonatomic, strong) WJShareCustomView *actionView;

@end

@implementation SYS_GoodsDetailVC

- (void)setShareContent:(id)shareContent{
    _shareContent = shareContent;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=NO;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor=DGHColor(240, 240, 240);
    
    self.title=@"商品详情";
    self.view.backgroundColor=DGHColor(240, 240, 240);
    session=[SYS_LoginModel instance].sessionInfo.session;
    //添加返回按钮
    [self addNav];
    
    //加载商品详情
    [self reloadDetail];
    
}

#pragma mark   webView
-(void) reloadDetail
{
    //创建webview，加载详情页
    _webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT-NavBarHeight)];
    _webView.delegate=self;
    _webView.scrollView.showsVerticalScrollIndicator=NO;
    _webView.scrollView.showsHorizontalScrollIndicator=NO;
    _webView.dataDetectorTypes = UIDataDetectorTypeNone;
    [self.view addSubview:_webView];
    session=[SYS_LoginModel instance].sessionInfo.session;
    NSURL *url;
    if (session.length>0) {
        url= [NSURL URLWithString:[NSString stringWithFormat:@"%@/pub/goodsView?goodsId=%@&&share=false&session=%@",SERVICE_IP,_goodsId,session]];
        
    }
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
    [request setHTTPMethod: @"GET"];
    _webView.scrollView.bounces=NO;
    [_webView loadRequest: request];
    
    _loadWebViewLayer = [[DGHLoadWebView alloc] init];
    _loadWebViewLayer.frame = CGRectMake(0, 42, SCREENWIDTH, 2);
    [self.navigationController.navigationBar.layer addSublayer:_loadWebViewLayer];
}

//如果是H5页面里面自带的返回按钮处理如下:
#pragma mark - webViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    NSString * requestString = [[request URL] absoluteString];
    requestString = [requestString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSArray *components = [requestString componentsSeparatedByString:@"|"];
    NSLog(@"=components=====%@",components);
    
    NSString *str1 = [components objectAtIndex:0];
    NSLog(@"str1:::%@",str1);
    
    NSArray *array2 = [str1 componentsSeparatedByString:@"/"];
    NSLog(@"array2:====%@",array2);
    
    NSInteger coun = array2.count;
    NSString *method = array2[coun-1];
    NSLog(@"method:===%@",method);
    
    NSArray *array3 = [str1 componentsSeparatedByString:@"="]; //从字符A中分隔成2个元素的数组
    NSLog(@"array:%@",array3); //结果是adfsfsfs和dfsdf
    NSInteger count3=array3.count;
    NSString *couponId=array3[count3-1];
    
    //获取H5页面里面按钮的操作方法,根据这个进行判断返回是内部的还是push的上一级页面
    if ([requestString hasPrefix:@"goback:"]) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self.webView goBack];
    }
    
    return YES;
}




/*
 * @brief url编码
 * @return 过滤后的字符串请求链接
 */
- (NSString *)encodeString:(NSString *)unencodedString{
    
    NSString *encodedString = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                              (CFStringRef)unencodedString,
                                                              NULL,
                                                              (CFStringRef)@"+$\"{}",
                                                              kCFStringEncodingUTF8));
    return encodedString;
}

/*
 * @brief url解码
 * @return 过滤后的字符串请求链接
 */
- (NSString *)decodeString:(NSString *)encodedString {
    
    NSString *decodedString  = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
                                                                                                                     (__bridge CFStringRef)encodedString,
                                                                                                                     CFSTR(""),
                                                                                                                     CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    
    return decodedString;
}


////返回
-(void) backToPrevious
{
    if ([self.webView canGoBack]) {
        [self.webView goBack];
        
    }else{
        [self.view resignFirstResponder];
        [self.navigationController popViewControllerAnimated:YES];
    }
}


#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
    
    [_loadWebViewLayer startLoad];
}


- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    
    [_loadWebViewLayer finishedLoadWithError:error];
    
}

- (void)dealloc {
    
    [_loadWebViewLayer closeTimer];
    [_loadWebViewLayer removeFromSuperlayer];
    _loadWebViewLayer = nil;
}


//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//
//    NSHTTPURLResponse *response = nil;
//    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
//
//    if (response.statusCode != 200) {
//        return NO;
//    }
//    return YES;
//}

#pragma mark   UI
-(void) addNav
{
    [self.navigationItem setHidesBackButton:YES];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"SYS_Public_back" highImage:nil title:nil target:self action:@selector(backToPrevious)];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
